#! /usr/bin/sdlbrt
finp$="tune.vgm"

'- .vgm to VortexTracker .txt converter - AY-3-8910 version
'- copyleft Paulo Silva, feb'16
'-------------------------------
'- bugs:
'- - an acute sound might appear because an issue related to volume 0 that VortexTracker doesnt support, R-- note used instead (and the converter should store which note/frequency is for a probable volume changing, and set back the note/frequency value back)
'- - doesn't read yet the author/title information
'- - 'L' will be useful for the loop point location
'- - probably will need a sample counter before converting
'- - missing noise support
'- - frequncy inaccuracy from the germanic notation formula probably fixed, needs more testing
'-------------------------------



ltxtm$=        "|...........|...........|...........|...........|...........|...........|...........|...........":ltxcr$=ltxtm$
'ltxtm$="....|..|--- .... ....|--- .... ....|--- .... ....":ltxcr$=ltxtm$
freq0=0:freq1=0:freq2=0:vol0=0:vol1=0:vol2=0
patsz=64:patc=0:patid=0:freqq=0
hdram=0x40:vgmv=0:veof=0
freqk0=0:freqk1=0:freqk2=0:freqk3=0:freqk4=0
volk0=0:volk1=0:volk2=0:volk3=0:volk4=0
dim hdrv[256]
dim wfc0[32],wfc1[32],wfc2[32],wfc3[32],wfc4[32]

  
if argc>2 then:finp$=argv(2):end if
fout2$=finp$+"_asciimod.txt":fout3$=finp$+"_asciimod_debug.txt"
  
frmc=0:frmr=300:trg=0
  
frmi=735
if argc>3 then:
  if argv(3)="1" or ucase$(argv(2))="-PAL" then
    frmi=882:end if:end if
  
debug=0
if argc>4 then:
  if argv(4)="1" or ucase$(argv(2))="-DEBUG" then
    debug=1:end if:end if
  
'- fix: 0x0FE=440hz=A-4 
function nttfrq$(freqb)
  e$=   "C-1C#1D-1D#1E-1F-1F#1G-1G#1A-1A#1B-1"
  e$=e$+"C-2C#2D-2D#2E-2F-2F#2G-2G#2A-2A#2B-2"
  e$=e$+"C-3C#3D-3D#3E-3F-3F#3G-3G#3A-3A#3B-3"
  e$=e$+"C-4C#4D-4D#4E-4F-4F#4G-4G#4A-4A#4B-4"
  e$=e$+"C-5C#5D-5D#5E-5F-5F#5G-5G#5A-5A#5B-5"
  e$=e$+"C-6C#6D-6D#6E-6F-6F#6G-6G#6A-6A#6B-6"
  e$=e$+"C-7C#7D-7D#7E-7F-7F#7G-7G#7A-7A#7B-7"
  e$=e$+"C-8C#8D-8D#8E-8F-8F#8G-8G#8A-8A#8B-8---"
  tmq=141-(int((log(freqb*.99)/log(2))*12))
  if tmq<1 then:tmq=1:end if
  if tmq>97 then:tmq=97:end if
  return mid$(e$,(tmq*3)-2,3)
  end function
  
open finp$ for input as #1
for i=0 to 255
  hdrv[i]=readbyte(1)
  next
close #1
  
hdram=0x040
vgmv=hdrv[8]+hdrv[9]*256
if vgmv>=0x0150 then:hdram=0x080:end if
if vgmv>=0x0160 then:hdram=0x0C0:end if
if vgmv>=0x0170 then:hdram=0x100:end if

'- wrong information???
ttsam=hdrv[0x18]+hdrv[0x19]*256+hdrv[0x1A]*65536+hdrv[0x1B]*16777216
hsam$="PlayOrder=L0"
for i=1 to (ttsam/(frmi*patsz))-1
  hsam$=hsam$+","+str$(i)
  next
  
open finp$ for input as #1
open fout2$ for output as #2
open fout3$ for output as #3

print #2,"[Module]"
print #2,"Format:mod"
print #2,"Title:"
print #2," "

for i=0 to 4
  print #2,"[SampleInfo"+hex$(i)+"]"
  print #2,"Name:SquareWave"
  print #2,"Lenght:0x0020"
  print #2,"Finetune:0"
  print #2,"Volume:0x40"
  print #2,"RepeatPoint:0x0000"
  print #2,"RepeatLenght:0x0020"
  print #2," " 
  next

print #2,"[MoreInfo]"
print #2,"SongLenght:8"
print #2,"VersionFlag1:0x00"
print #2,"PlayOrder:0,1,2,3,4,5,6,7"
print #2,"VersionFlag2:8CHN"
print #2," "
print #2,"[Pattern0]"
print #2,"ModPlug Tracker MOD"

'- the amount of header bytes depends on vgm format version
for eee=0 to hdram-1:q0=readbyte(1):next  '- read offset byte first
txou1$="    #("+str$(0)+")"
  
while veof=0:
  q0=readbyte(1)
  
  '- vgm eof command
  if q0=0x66 then:
    veof=1
    print #3,"--vgm-eof--"
    print #2," "
    end if
  
  '- delay 1 byte (1..16 samples)
  if bitwiseand(q0,0x70)=0x70 then
    frmc=frmc+bitwiseand(q0,0xF)+1
    txou1$="    #("+str$(frmc)+")"
    'print #3,txou1$
    end if
  
  '- delay 3 bytes (0..65535 samples)
  if q0=0x61 then
    q0=readbyte(1)
    frmc=frmc+q0
    q0=readbyte(1)
    frmc=frmc+(q0*256)
    txou1$="    #("+str$(frmc)+")"
    'print #3,txou1$
    end if
  
  '- 1 ntsc frame delay, 735 samples
  if q0=0x62 then
    frmc=frmc+735
    txou1$="    #("+str$(frmc)+")"
    'print #3,txou1$
    end if
  
  '- 1 pal frame delay, 882 samples
  if q0=0x63 then
    frmc=frmc+882
    txou1$="    #("+str$(frmc)+")"
    'print #3,txou1$
    end if
  
  
  
  '- updates frameout pulses
  while frmr<frmc
  
    '- writes F7F and F01 in the firsts lines of the first pattern
    if (patc<1 and patid=0) then
      ltxcr$=replace$(93,ltxcr$,"F7F")
      end if
    if (patc=1 and patid=0) then
      ltxcr$=replace$(93,ltxcr$,"F01")
      end if
  
    '- creates a new pattern  
    if patc>(patsz-1) then:
      print #2," "
      patid=patid+1:patc=0
      txou9$="[Pattern"+str$(patid)+"]"
      print #2,txou9$
      print #2,"ModPlug Tracker MOD"
      end if
  
    '- writes a pattern line in each frame
    if trg=0 then
      print #3,"--frameout-unchanged--"
      print #2,ltxtm$
    else
      print #3,"--frameout--"
      print #2,ltxcr$
      ltxcr$=ltxtm$
      trg=0
      end if
  
    frmr=frmr+frmi
    patc=patc+1
    end while
  
  
  
  '- token 0xA0 gets registers and values to AY-3-8910 output
  if q0=0xA0 then
    q0=readbyte(1)
  
    '- channel 0, fine
    if q0=0 then
      q0=readbyte(1)
      freq0= bitwiseor((bitwiseand(freq0,0xF00)),(bitwiseand(q0,0x0FF)))
      ltxtm1$=ltxtm$
      ltxtm2$=replace$(1,ltxtm1$,nttfrq$(freq0)+"01")
      ltxcr$=replace$(1,ltxcr$,nttfrq$(freq0)+"01")
      print #3,ltxtm2$
      trg=1
      end if
  
    '- channel 0, coarse
    if q0=1 then
      q0=readbyte(1)
      freq0= bitwiseor ( (bitwiseand(q0*256,0xF00)),(bitwiseand(freq0,0x0FF)) )
      ltxtm1$=ltxtm$
      ltxtm2$=replace$(1,ltxtm1$,nttfrq$(freq0)+"01")
      ltxcr$=replace$(1,ltxcr$,nttfrq$(freq0)+"01")
      print #3,ltxtm2$
      trg=1
      end if
  
    '- channel 1, fine
    if q0=2 then
      q0=readbyte(1)
      freq1= bitwiseor ( (bitwiseand(freq1,0xF00)),(bitwiseand(q0,0x0FF)) )
      ltxtm1$=ltxtm$
      ltxtm2$=replace$(13,ltxtm1$,nttfrq$(freq1)+"01")
      ltxcr$=replace$(13,ltxcr$,nttfrq$(freq1)+"01")
      print #3,ltxtm2$
      trg=1
      end if
  
    '- channel 1, coarse
    if q0=3 then
      q0=readbyte(1)
      freq1= bitwiseor ( (bitwiseand(q0*256,0xF00)),(bitwiseand(freq1,0x0FF)) )
      ltxtm1$=ltxtm$
      ltxtm2$=replace$(13,ltxtm1$,nttfrq$(freq1)+"01")
      ltxcr$=replace$(13,ltxcr$,nttfrq$(freq1)+"01")
      print #3,ltxtm2$
      trg=1
      end if
  
    '- channel 2, fine
    if q0=4 then
      q0=readbyte(1)
      freq2= bitwiseor ( (bitwiseand(freq2,0xF00)),(bitwiseand(q0,0x0FF)) )
      ltxtm1$=ltxtm$
      ltxtm2$=replace$(25,ltxtm1$,nttfrq$(freq2)+"01")
      ltxcr$=replace$(25,ltxcr$,nttfrq$(freq2)+"01")
      print #3,ltxtm2$
      trg=1
      end if
  
    '- channel 2, coarse
    if q0=5 then
      q0=readbyte(1)
      freq2= bitwiseor ( (bitwiseand(q0*256,0xF00)),(bitwiseand(freq2,0x0FF)) )
      ltxtm1$=ltxtm$
      ltxtm2$=replace$(25,ltxtm1$,nttfrq$(freq2)+"01")
      ltxcr$=replace$(25,ltxcr$,nttfrq$(freq2)+"01")
      print #3,ltxtm2$
      trg=1
      end if
  
    '?????
    if q0=6 then
      print #3,ltxtm$
      end if
  
    '?????
    'if q0=7 then
    '  q0=readbyte(1)
    '  ltxtm1$=ltxtm$
    '  ltxtm2$=left$(ltxtm1$,31)+  right$(bin$(512+q0),8)  +right$(ltxtm1$,2)+txou1$
    '  print #3,ltxtm2$
    '  trg=1
    '  end if
  
    '- channel 1, volume
    if q0=8 then
      q0=readbyte(1)
      vol0= bitwiseand(q0,0xF)
      ltxtm1$=ltxtm$
      ltxtm2$=replace$(9,ltxtm1$,"C"+ucase$(right$(hex$(0x100+vol0*4),2)))
      ltxcr$=replace$(9,ltxcr$,"C"+ucase$(right$(hex$(0x100+vol0*4),2)))
      print #3,ltxtm2$
      trg=1
      end if
  
    '- channel 2, volume
    if q0=9 then
      q0=readbyte(1)
      vol1= bitwiseand(q0,0xF)
      ltxtm1$=ltxtm$
      ltxtm2$=replace$(21,ltxtm1$,"C"+ucase$(right$(hex$(0x100+vol1*4),2)))
      ltxcr$=replace$(21,ltxcr$,"C"+ucase$(right$(hex$(0x100+vol1*4),2)))
      print #3,ltxtm2$
      trg=1
      end if
  
    '- channel 3, volume
    if q0=10 then
      q0=readbyte(1)
      vol2= bitwiseand(q0,0xF)
      ltxtm1$=ltxtm$
      ltxtm2$=replace$(33,ltxtm1$,"C"+ucase$(right$(hex$(0x100+vol2*4),2)))
      ltxcr$=replace$(33,ltxcr$,"C"+ucase$(right$(hex$(0x100+vol2*4),2)))
      print #3,ltxtm2$
      trg=1
      end if
  
    end if


  '- token 0xD2 gets registers and values to K051649 output
  if q0=0xD2 then
    q0=readbyte(1)

    if q0=0x00 then '- write waveform
      q0=readbyte(1)
      if int(q0/32)=0 then
        q1=readbyte(1)
        wfc0[bitwiseand(q0,31)]=q1
        end if
      if int(q0/32)=1 then
        q1=readbyte(1)
        wfc1[bitwiseand(q0,31)]=q1
        end if
      if int(q0/32)=2 then
        q2=readbyte(1)
        wfc2[bitwiseand(q0,31)]=q2
        end if
      if int(q0/32)=3 then
        q3=readbyte(1)
        wfc3[bitwiseand(q0,31)]=q3
        end if
      if int(q0/32)=3 then
        q4=readbyte(1)
        wfc4[bitwiseand(q0,31)]=q4
        end if



      end if

    if q0=0x01 then '- set frequency
      q0=readbyte(1)
      if q0=0 then
        q0=readbyte(1)
        freqk0= bitwiseor((bitwiseand(freqk0,0xF00)),(bitwiseand(q0,0x0FF)))
        ltxtm1$=ltxtm$
        ltxtm2$=replace$(37,ltxtm1$,nttfrq$(freqk0)+"02")
        ltxcr$=replace$(37,ltxcr$,nttfrq$(freqk0)+"02")
        print #3,ltxtm2$
        trg=1
        end if
      if q0=1 then
        q0=readbyte(1)
        freqk0= bitwiseor ( (bitwiseand(q0*256,0xF00)),(bitwiseand(freqk0,0x0FF)) )
        ltxtm1$=ltxtm$
        ltxtm2$=replace$(37,ltxtm1$,nttfrq$(freqk0)+"02")
        ltxcr$=replace$(37,ltxcr$,nttfrq$(freqk0)+"02")
        print #3,ltxtm2$
        trg=1
        end if
      if q0=2 then
        q0=readbyte(1)
        freqk1= bitwiseor((bitwiseand(freqk1,0xF00)),(bitwiseand(q0,0x0FF)))
        ltxtm1$=ltxtm$
        ltxtm2$=replace$(49,ltxtm1$,nttfrq$(freqk1)+"03")
        ltxcr$=replace$(49,ltxcr$,nttfrq$(freqk1)+"03")
        print #3,ltxtm2$
        trg=1
        end if
      if q0=3 then
        q0=readbyte(1)
        freqk1= bitwiseor ( (bitwiseand(q0*256,0xF00)),(bitwiseand(freqk1,0x0FF)) )
        ltxtm1$=ltxtm$
        ltxtm2$=replace$(49,ltxtm1$,nttfrq$(freqk1)+"03")
        ltxcr$=replace$(49,ltxcr$,nttfrq$(freqk1)+"03")
        print #3,ltxtm2$
        trg=1
        end if
      if q0=4 then
        q0=readbyte(1)
        freqk2= bitwiseor((bitwiseand(freqk2,0xF00)),(bitwiseand(q0,0x0FF)))
        ltxtm1$=ltxtm$
        ltxtm2$=replace$(61,ltxtm1$,nttfrq$(freqk2)+"04")
        ltxcr$=replace$(61,ltxcr$,nttfrq$(freqk2)+"04")
        print #3,ltxtm2$
        trg=1
        end if
      if q0=5 then
        q0=readbyte(1)
        freqk2= bitwiseor ( (bitwiseand(q0*256,0xF00)),(bitwiseand(freqk2,0x0FF)) )
        ltxtm1$=ltxtm$
        ltxtm2$=replace$(61,ltxtm1$,nttfrq$(freqk2)+"04")
        ltxcr$=replace$(61,ltxcr$,nttfrq$(freqk2)+"04")
        print #3,ltxtm2$
        trg=1
        end if
      if q0=6 then
        q0=readbyte(1)
        freqk3= bitwiseor((bitwiseand(freqk3,0xF00)),(bitwiseand(q0,0x0FF)))
        ltxtm1$=ltxtm$
        ltxtm2$=replace$(73,ltxtm1$,nttfrq$(freqk3)+"05")
        ltxcr$=replace$(73,ltxcr$,nttfrq$(freqk3)+"05")
        print #3,ltxtm2$
        trg=1
        end if
      if q0=7 then
        q0=readbyte(1)
        freqk3= bitwiseor ( (bitwiseand(q0*256,0xF00)),(bitwiseand(freqk3,0x0FF)) )
        ltxtm1$=ltxtm$
        ltxtm2$=replace$(73,ltxtm1$,nttfrq$(freqk3)+"05")
        ltxcr$=replace$(73,ltxcr$,nttfrq$(freqk3)+"05")
        print #3,ltxtm2$
        trg=1
        end if
      if q0=8 then
        q0=readbyte(1)
        freqk4= bitwiseor((bitwiseand(freqk4,0xF00)),(bitwiseand(q0,0x0FF)))
        ltxtm1$=ltxtm$
        ltxtm2$=replace$(85,ltxtm1$,nttfrq$(freqk4)+"05")
        ltxcr$=replace$(85,ltxcr$,nttfrq$(freqk4)+"05")
        print #3,ltxtm2$
        trg=1
        end if
      if q0=9 then
        q0=readbyte(1)
        freqk4= bitwiseor ( (bitwiseand(q0*256,0xF00)),(bitwiseand(freqk4,0x0FF)) )
        ltxtm1$=ltxtm$
        ltxtm2$=replace$(85,ltxtm1$,nttfrq$(freqk4)+"05")
        ltxcr$=replace$(85,ltxcr$,nttfrq$(freqk4)+"05")
        print #3,ltxtm2$
        trg=1
        end if
      end if

    if q0=0x02 then '- set volume
      q0=readbyte(1)

      if q0=0 then
        q0=readbyte(1)
        volk0= bitwiseand(q0,0xF)
        ltxtm1$=ltxtm$
        ltxtm2$=replace$(45,ltxtm1$,"C"+ucase$(right$(hex$(0x100+volk0*4),2)))
        ltxcr$=replace$(45,ltxcr$,"C"+ucase$(right$(hex$(0x100+volk0*4),2)))
        print #3,ltxtm2$
        trg=1
        end if

      if q0=1 then
        q0=readbyte(1)
        volk1= bitwiseand(q0,0xF)
        ltxtm1$=ltxtm$
        ltxtm2$=replace$(57,ltxtm1$,"C"+ucase$(right$(hex$(0x100+volk1*4),2)))
        ltxcr$=replace$(57,ltxcr$,"C"+ucase$(right$(hex$(0x100+volk1*4),2)))
        print #3,ltxtm2$
        trg=1
        end if

      if q0=2 then
        q0=readbyte(1)
        volk2= bitwiseand(q0,0xF)
        ltxtm1$=ltxtm$
        ltxtm2$=replace$(69,ltxtm1$,"C"+ucase$(right$(hex$(0x100+volk2*4),2)))
        ltxcr$=replace$(69,ltxcr$,"C"+ucase$(right$(hex$(0x100+volk2*4),2)))
        print #3,ltxtm2$
        trg=1
        end if

      if q0=3 then
        q0=readbyte(1)
        volk3= bitwiseand(q0,0xF)
        ltxtm1$=ltxtm$
        ltxtm2$=replace$(81,ltxtm1$,"C"+ucase$(right$(hex$(0x100+volk3*4),2)))
        ltxcr$=replace$(81,ltxcr$,"C"+ucase$(right$(hex$(0x100+volk3*4),2)))
        print #3,ltxtm2$
        trg=1
        end if

      if q0=4 then
        q0=readbyte(1)
        volk4= bitwiseand(q0,0xF)
        ltxtm1$=ltxtm$
        ltxtm2$=replace$(93,ltxtm1$,"C"+ucase$(right$(hex$(0x100+volk4*4),2)))
        ltxcr$=replace$(93,ltxcr$,"C"+ucase$(right$(hex$(0x100+volk4*4),2)))
        print #3,ltxtm2$
        trg=1
        end if


      end if

    end if





  if eof(1)<>0 then:veof=1:end if
  wend

print #2," "
print #2,"[Sample0]"
print #2,"7F7F7F7F7F7F7F7F7F7F7F7F7F7F7F7F80808080808080808080808080808080"
print #2," "

txousm$=""
for i=0 to 31
  txousm$=txousm$+ucase$(right$(hex$(0x180+wfc0[i]),2))
  next
print #2,"[Sample1]"
print #2,txousm$
print #2," "

txousm$=""
for i=0 to 31
  txousm$=txousm$+ucase$(right$(hex$(0x180+wfc1[i]),2))
  next
print #2,"[Sample2]"
print #2,txousm$
print #2," "

txousm$=""
for i=0 to 31
  txousm$=txousm$+ucase$(right$(hex$(0x180+wfc2[i]),2))
  next
print #2,"[Sample3]"
print #2,txousm$
print #2," "

txousm$=""
for i=0 to 31
  txousm$=txousm$+ucase$(right$(hex$(0x180+wfc3[i]),2))
  next
print #2,"[Sample4]"
print #2,txousm$
print #2," "

txousm$=""
for i=0 to 31
  txousm$=txousm$+ucase$(right$(hex$(0x180+wfc4[i]),2))
  next
print #2,"[Sample5]"
print #2,txousm$
print #2," "




close #1:close #2:close #3
  
if debug=0 then:
  shell("rm "+fout3$)
  end if

